//
//  MessageDetailV.m
//  carcareIOS
//
//  Created by 王然 on 16/9/1.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageListV.h"

#import "DetectionUrlView.h"
#import "SystemHelper.h"

#define View_Add_Height (Label_Y * 3 + Label_Height)

#define Label_Color_CarList (COLOR_HEXSTRING(@"#191919"))
#define Label_Gray_Color_CarList (COLOR_HEXSTRING(@"#ababab"))

#define Label_Title_Font (FONT_DEFAULT_Light(15))

@interface MessageListV()<DetectionUrlViewDelegate>

@property (nonatomic , strong) UILabel * timeL;

@property (nonatomic , strong) DetectionUrlView * titleL;

@property (nonatomic , assign) BOOL isFillBackColor;//背景填充色

@property (nonatomic , strong) MessageModel * model;
@end

@implementation MessageListV

- (instancetype)initWithWidth:(CGFloat)width
{
    self = [super initWithFrame:CGRectMake(0, 0, width, View_Add_Height)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createUI];
    }
    return self;
}
- (void)createUI
{
    self.timeL = [[UILabel labelWithFrame:CGRectMake(Label_X, Label_Y, self.width - Label_X * 2, Label_Height) font:FONT_DEFAULT_Light(13) text:@"" textColor:Label_Gray_Color_CarList] setupOnView:self];
    self.timeL.backgroundColor = [UIColor clearColor];
    __weak __typeof(self) wself = self;
    self.titleL = [[[DetectionUrlView alloc]initWithMaxWidth:self.timeL.width resetSizeFinish:^(TTTAttributedLabel *label) {
        [wself.titleL setOrigin:CGPointMake(wself.timeL.left, wself.timeL.bottom + Label_Y)];
        wself.height = label.size.height + View_Add_Height ;
    }]setupOnView:self];
    self.titleL.backgroundColor = [UIColor clearColor];
    self.titleL.delegate = self; 
    
}
- (void)reloadCarListDataWithModel:(MessageModel *)model
{
    self.model = model;
    
    self.timeL.textColor = Label_Gray_Color_CarList;
    [self.titleL resetDetectionTextColor:(model.STATUS ? Label_Gray_Color_CarList : Label_Color_CarList) highlightTextColor:(model.STATUS ? Label_Gray_Color_CarList : Label_Color_CarList) textFont:Label_Title_Font canClickColor:[UIColor blueColor] isClickHaveLine:YES];
    self.timeL.text = model.carMessageTime;
    [self.titleL detectionWithStr:model.ABSTRACT clickTextFont:Label_Title_Font textCheckType:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber];
    
    self.isFillBackColor = YES;
    [self setNeedsDisplay];
}
- (void)reloadListDataWithModel:(MessageModel *)model type:(MessageCenter_Type)pageType
{
    self.timeL.textColor = Label_Date_Color_List;
    UIColor * wordColor = (((pageType == MessageCenter_System && model.isExpired) || (pageType == MessageCenter_Service && model.STATUS)) ? Label_Date_Color_List : Label_Gray_Color_List);
    [self.titleL resetDetectionTextColor:wordColor highlightTextColor:wordColor textFont:Label_Title_Font canClickColor:[UIColor blueColor] isClickHaveLine:YES];
    
    self.timeL.text = (pageType == MessageCenter_Service ? model.messageTimeDate : (pageType == MessageCenter_Insurance ? model.dateTime : [NSString stringWithFormat:@"%@ %@",model.dateTime,model.carMessageTime]));
    [self.titleL detectionWithStr:model.ABSTRACT clickTextFont:Label_Title_Font textCheckType:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber];
    
    self.isFillBackColor = NO;
    [self setNeedsDisplay];
}
+ (CGFloat)getCarListCellHeightWithModel:(MessageModel *)model
{
    NSString *str = (model ? model.ABSTRACT : @"");
    
    NSDictionary *attributes = @{NSFontAttributeName:Label_Title_Font};
    
    CGFloat height = [str boundingRectWithSize:CGSizeMake(CarListV_Width - Label_X * 2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
    
    return View_Add_Height + height ;
}
+ (CGFloat)getListCellHeightWithModel:(MessageModel *)model;
{
    NSString *str = (model ? model.ABSTRACT : @"");
    NSDictionary *attributes = @{NSFontAttributeName:Label_Title_Font};
    CGFloat height = [str boundingRectWithSize:CGSizeMake(ListV_Width - Label_X * 2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
    return View_Add_Height + height;
}
#pragma mark - TTTAttributedLabel delegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
    [SYSTEMHELPER callPhone:phoneNumber];
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    if(self.clickLabelURL)self.clickLabelURL([url absoluteString]);
}
- (void)drawRect:(CGRect)rect
{
    if (self.isFillBackColor && self.model.propertyDictionaryValue.count > 0) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGFloat triangleH = 12;
        CGFloat redius = 4;
        CGFloat startY = self.timeL.bottom - (self.timeL.height - triangleH) / 2;
        CGContextMoveToPoint(context, Triangle_W * 2, startY);
        CGContextAddArcToPoint(context, Triangle_W, startY - triangleH / 2, Triangle_W + Triangle_W / 2, startY - triangleH / 4 * 3, redius / 2);
        CGContextAddLineToPoint(context, Triangle_W * 2, startY - triangleH );
        CGContextAddArcToPoint(context, Triangle_W * 2, 0, CarListV_Width / 2 , 0, redius);
        CGFloat rightX = CarListV_Width - Triangle_W * 2;
        CGFloat endY = [MessageListV getCarListCellHeightWithModel:self.model];
        CGContextAddArcToPoint(context, rightX , 0, rightX, endY / 2, redius);
        
        CGContextAddArcToPoint(context, rightX, endY , rightX / 2, endY , redius);
        CGContextAddArcToPoint(context, Triangle_W * 2, endY, Triangle_W * 2, startY, redius);
        CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextDrawPath(context, kCGPathEOFillStroke);
//        CGContextClosePath(context);
    }
}
@end
